package com.ipmirror.navigation;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import com.icesoft.faces.component.menubar.MenuItem;
import com.ipmirror.common.base.AbstractBaseBean;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.annotation.PostConstruct;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 * Created by denny User: denny Date: Oct 11, 2010 Time: 9:40:38 AM
 */
public class NavigationBean extends AbstractBaseBean {
	private static final Logger log = Logger.getLogger(NavigationBean.class);

	/**
	 * Value of currently displayed page
	 */
	private String currentPage;

	/**
	 * Value of currently displayed page Key
	 */
	private String currentPageKey;

	/**
	 * Value of currently displayed page title
	 */
	private String currentPageTitle;

	/**
	 * Value of currently displayed page before encountering exception
	 */
	private String lastPage;

	/**
	 * Value of currently displayed page Key before encountering exception
	 */
	private String lastPageKey;

	@PostConstruct
	public void init() {
        super.init();
        log.info("Init Navigation Bean");
	}

	/**
	 * Listener for  Menu selection
	 */
	public void navigationListener(ActionEvent e) {
		String key = ((MenuItem) e.getSource()).getId();
		if (StringUtils.isEmpty(key))
			key = NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId();
		navigateNextPage(key);
		reload();
	}

	/**
	 * Listener for  Link selection Patient Context will be reset
	 */
	public void navigationLinkListener(ActionEvent e) {
		String key = ((HtmlCommandLink) e.getSource()).getId();
		if (StringUtils.isEmpty(key))
			key = NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId();
		navigateNextPage(key);
		reload();
	}

	/**
	 * Listener for  Shortcut Link Patient Context will be reset
	 */
	public void navigationShortcutListener(ActionEvent e) {
		String key = ((HtmlCommandLink) e.getSource()).getId();
		if (StringUtils.isEmpty(key))
			key = NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId();
		navigateNextPage(key);
		// resetPatientContext();
		reload();
	}

	/**
	 * Listener for  HTMLCommand
	 */
	public void navigationCommandListener(ActionEvent e) {
		String key = ((HtmlCommandButton) e.getSource()).getId();
		if (StringUtils.isEmpty(key))
			key = NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId();
		navigateNextPage(key);
		// resetPatientContext();
		reload();
	}

	/**
	 * Method to navigate from Managed Bean Patient Context will be reset
	 */
	public void navigate(String key, boolean resetPatientContextInd) {
		if (StringUtils.isEmpty(key))
			key = NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId();
		navigateNextPage(key);
		reload();
	}

	/**
	 * Listener for HTMLCommand Patient Context will be reset
	 */
	public void navigationCommandResetListener(ActionEvent e) {
		String key = ((HtmlCommandButton) e.getSource()).getId();
		if (StringUtils.isEmpty(key))
			key = NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId();
		navigateNextPage(key);
		reload();
	}

	private void navigateNextPage(String key) {
		this.lastPage = this.currentPage;
		this.lastPageKey = this.currentPageKey;
//		log.info("key is " + key);
		this.currentPage = NavigationEnum.fromString(key).getMenuLink();
//		log.info("currentPage is " + currentPage);
		this.currentPageTitle = NavigationEnum.fromString(key).getPageTitle();
		this.currentPageKey = key;
	}

	/**
	 * Listener for Exception
	 */
	public void navigationExceptionListener() {
		String key = NavigationEnum.DEFAULT_EXCEPTION.getMenuId();
		this.currentPage = NavigationEnum.fromString(key).getMenuLink();
		this.currentPageTitle = NavigationEnum.fromString(key).getPageTitle();
		this.currentPageKey = key;
	}

	/**
	 *Get Current Page
	 */
	public String getCurrentPage() {
		if (StringUtils.isBlank(this.currentPage)) {
			this.currentPage = NavigationEnum.fromString(
					NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId()).getMenuLink();
		}
		return currentPage;
	}

	/**
	 *Get Current Page Title
	 */
	public String getCurrentPageTitle() {
		if (StringUtils.isBlank(this.currentPage)) {
			this.currentPageTitle = NavigationEnum.fromString(
					NavigationEnum.REGISTRY_CONSOLE_HOME.getMenuId()).getPageTitle();
		}
		if (StringUtils.isBlank(currentPageTitle)) {
			this.currentPageTitle = NavigationEnum.REGISTRY_CONSOLE_HOME.getPageTitle();
		}
		return currentPageTitle;
	}

	/**
	 *Reload the Page
	 */
	private void reload() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		NavigationHandler navHandler = facesContext.getApplication()
				.getNavigationHandler();
		navHandler.handleNavigation(facesContext, null, "page.reload");
	}

	public String getCurrentPageKey() {
		return currentPageKey;
	}

	public void setCurrentPageKey(String currentPageKey) {
		this.currentPageKey = currentPageKey;
	}

	public String getLastPageKey() {
		return lastPageKey;
	}

	public void setLastPageKey(String lastPageKey) {
		this.lastPageKey = lastPageKey;
	}

	private boolean onSubmitModalRendered = false;

	public boolean isOnSubmitModalRendered() {
		return onSubmitModalRendered;
	}

	public void setOnSubmitModalRendered(boolean onSubmitModalRendered) {
		this.onSubmitModalRendered = onSubmitModalRendered;
	}

}
